package impl

import (
	"context"
	"errors"
	"fmt"
	"go-caipu/pkg/services/admin/systemlog"
	"go-caipu/pkg/services/admin/systemlog/models"
	"go-caipu/pkg/services/dto"
	"gorm.io/gorm"
	"time"
)

func (s sqlStore) GetSysOperaLogPage(ctx context.Context, cmd systemlog.GetSysOperaLogPageCommand) (result []systemlog.SysOperaLog, num int64, err error) {
	var items []models.SysOperaLog
	err = s.db.Model(&models.SysOperaLog{}).Scopes(
		dto.MakeCondition(cmd.GetNeedSearch()),
		dto.Paginate(cmd.GetPageSize(), cmd.GetPageIndex()),
	).Omit("request_body", "response_body", "request_headers", "response_headers").Find(&items).Limit(-1).Offset(-1).Count(&num).Error
	if err != nil {
		return
	}
	for _, v := range items {
		result = append(result, ConvertOperaToDTO(v))
	}
	return
}
func ConvertOperaToDTO(model models.SysOperaLog) systemlog.SysOperaLog {
	result := systemlog.SysOperaLog{
		Id:              model.Id,
		Description:     model.Description,
		Module:          model.Module,
		Os:              model.Os,
		Address:         model.Address,
		Browser:         model.Browser,
		Ip:              model.Ip,
		Method:          model.Method,
		RequestBody:     model.RequestBody,
		RequestHeaders:  model.RequestHeaders,
		RequestUrl:      model.RequestUrl,
		ResponseBody:    model.ResponseBody,
		ResponseHeaders: model.ResponseHeaders,
		StatusCode:      model.StatusCode,
		Remark:          model.Remark,
		TraceId:         model.TraceId,
		TimeTaken:       model.TimeTaken,
		CreateUser:      model.CreateUser,
		CreatedAt:       model.CreatedAt.Format("2006-01-02 15:04:05"),
		//ControlBy:  model.ControlBy,

	}
	return result
}
func ConvertOperToDT(model *systemlog.SysOperaLog, cmd *models.SysOperaLog) {
	cmd.Id = model.Id
	cmd.Module = model.Module
	cmd.Description = model.Description
	cmd.Os = model.Os
	cmd.Address = model.Address
	cmd.Browser = model.Browser
	cmd.Ip = model.Ip
	cmd.Method = model.Method
	cmd.RequestBody = model.RequestBody
	cmd.RequestHeaders = model.RequestHeaders
	cmd.RequestUrl = model.RequestUrl
	cmd.ResponseBody = model.ResponseBody
	cmd.ResponseHeaders = model.ResponseHeaders
	cmd.StatusCode = model.StatusCode
	cmd.Remark = model.Remark
	cmd.TraceId = model.TraceId
	cmd.TimeTaken = model.TimeTaken
	cmd.CreateUser = model.CreateUser
	cmd.CreateBy = model.CreateBy

}

func (s sqlStore) InsertOperLog(ctx context.Context, cmd *systemlog.SysOperaLog) (err error) {
	tx := s.db.Begin()
	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()
	var data models.SysOperaLog
	ConvertOperToDT(cmd, &data)
	data.CreatedAt = time.Now()
	//data.CreateBy = cmd.CreateBy

	err = tx.Create(&data).Error
	if err != nil {
		s.logger.Log(fmt.Sprintf("insert  operlog  error:%s", err.Error()))
		return err
	}
	return nil
}
func (s sqlStore) GetSysOperaLog(ctx context.Context, cmd systemlog.GetSysOperaLogCommand) (result systemlog.SysOperaLog, err error) {
	var data models.SysOperaLog
	err = s.db.Model(&data).First(&data, cmd.ID).Error
	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
		err = errors.New(fmt.Sprintf("Id=%d record Not found", cmd.ID))
		return
	}
	if err != nil {
		s.logger.Log(fmt.Sprintf("getoperalog =%d  error:%s", cmd.ID, err.Error()))
		return
	}
	return ConvertOperaToDTO(data), nil
}
